"""
用户设置相关的数据库操作函数
"""

from .db import get_db_connection, close_connection

async def update_password(user_id, current_password, new_password):
    """
    更新用户密码
    
    Args:
        user_id: 用户ID
        current_password: 当前密码
        new_password: 新密码
    
    Returns:
        bool: 是否更新成功
    """
    connection = None
    try:
        connection = await get_db_connection()
        async with connection.cursor() as cursor:
            # 先验证当前密码
            sql = "SELECT password FROM users WHERE id = %s AND password = %s"
            await cursor.execute(sql, [user_id, current_password])
            if not await cursor.fetchone():
                return False
                
            # 更新新密码
            sql = "UPDATE users SET password = %s WHERE id = %s"
            await cursor.execute(sql, [new_password, user_id])
            await connection.commit()
            return True
    except Exception as e:
        print(f"更新密码失败: {str(e)}")
        return False
    finally:
        await close_connection(connection)

async def update_phone(user_id, current_password, new_phone):
    """
    更新用户手机号
    
    Args:
        user_id: 用户ID
        current_password: 当前密码
        new_phone: 新手机号
    
    Returns:
        bool: 是否更新成功
    """
    connection = None
    try:
        connection = await get_db_connection()
        async with connection.cursor() as cursor:
            # 先验证当前密码
            sql = "SELECT password FROM users WHERE id = %s AND password = %s"
            await cursor.execute(sql, [user_id, current_password])
            if not await cursor.fetchone():
                return False
                
            # 更新手机号
            sql = "UPDATE users SET phone = %s WHERE id = %s"
            await cursor.execute(sql, [new_phone, user_id])
            await connection.commit()
            return True
    except Exception as e:
        print(f"更新手机号失败: {str(e)}")
        return False
    finally:
        await close_connection(connection)

async def update_username(user_id, new_username):
    """
    更新用户名
    
    Args:
        user_id: 用户ID
        new_username: 新用户名
    
    Returns:
        bool: 是否更新成功
    """
    connection = None
    try:
        connection = await get_db_connection()
        async with connection.cursor() as cursor:
            sql = "UPDATE users SET username = %s WHERE id = %s"
            await cursor.execute(sql, [new_username, user_id])
            await connection.commit()
            return True
    except Exception as e:
        print(f"更新用户名失败: {str(e)}")
        return False
    finally:
        await close_connection(connection)

async def get_user_info(user_id):
    """
    获取用户信息
    
    Args:
        user_id: 用户ID
    
    Returns:
        dict: 用户信息字典，包含用户名和手机号
    """
    connection = None
    try:
        connection = await get_db_connection()
        async with connection.cursor() as cursor:
            sql = "SELECT username, phone FROM users WHERE id = %s"
            await cursor.execute(sql, [user_id])
            result = await cursor.fetchone()
            if result:
                return {
                    "username": result["username"],
                    "phone": result["phone"]
                }
            return None
    except Exception as e:
        print(f"获取用户信息失败: {str(e)}")
        return None
    finally:
        await close_connection(connection)
